﻿using System;
using cfg.ability;

namespace QFramework.Monk
{
    public class CoinUpItem:IController
    {

        public EasyEvent onChanged = new EasyEvent();
        
        public Ability mAbilityConfig;
        
        private Action<CoinUpItem> mUpgradeAction;

        public AbilityType mAbilityType;
        
        private ConfigModel mConfigModel;
        public void OnUpgrade()
        {
            mUpgradeAction?.Invoke(this);
            RefreshConfig();
        }
        public CoinUpItem WithAbilityType(AbilityType abilityType)
        {
            mAbilityType = abilityType;
            mConfigModel = this.GetModel<ConfigModel>();
            RefreshConfig();
            return this;
        }
        public CoinUpItem WithUpgradeAction(Action<CoinUpItem> upgradeAction)
        {
            mUpgradeAction = upgradeAction;
            return this;
        }
        
        public void RefreshConfig()
        {
            var curLv = 0;
            switch (mAbilityType)
            {
                case AbilityType.Exp:
                    curLv = AppStart.expDropLv.Value;
                    break;
                case AbilityType.Coin:
                    curLv = AppStart.coinDropLv.Value;
                    break;
                case AbilityType.MaxHp:
                    curLv = AppStart.hpLv.Value;
                    break;
                case AbilityType.Attack:
                    curLv = AppStart.attackLv.Value;
                    break;
            }
            mAbilityConfig = mConfigModel.GetNextAbilityByType(curLv,mAbilityType);
            //触发发刷新事件
            onChanged.Trigger();
        }

        public bool CheckCanUpgrade()
        {
            var curLv = 0;
            switch (mAbilityType)
            {
                case AbilityType.Exp:
                    curLv = AppStart.expDropLv.Value;
                    break;
                case AbilityType.Coin:
                    curLv = AppStart.coinDropLv.Value;
                    break;
                case AbilityType.MaxHp:
                    curLv = AppStart.hpLv.Value;
                    break;
                case  AbilityType.Attack:
                    curLv = AppStart.attackLv.Value;
                    break;
            }
            var abilityGroup =mConfigModel.GetAbilityGroupByType(mAbilityType);
            
            //先检查等级
            if (curLv>=abilityGroup.MaxLv)
            {
                return false;
            }
            //再检查金币
            return AppStart.goldNum.Value>=mAbilityConfig.Price;
            
        }

        public IArchitecture GetArchitecture()
        {
            return  AppStart.Interface;
        }
    }
}